package com.qqtech.qquan.coupon.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.common.util.UUIDUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.coupon.cache.CouponSysParamCache;
import com.qqtech.qquan.coupon.dao.CouponUserDao;
import com.qqtech.qquan.coupon.enums.CouponBaseBizzTypeDictEnum;
import com.qqtech.qquan.coupon.enums.CouponConst;
import com.qqtech.qquan.coupon.enums.CouponUserObtainWayDictEnum;
import com.qqtech.qquan.coupon.enums.CouponUserStatusDictEnum;
import com.qqtech.qquan.coupon.model.CouponBaseVo;
import com.qqtech.qquan.coupon.model.CouponSysParamVo;
import com.qqtech.qquan.coupon.model.CouponUser;
import com.qqtech.qquan.coupon.model.CouponUserQuery;
import com.qqtech.qquan.coupon.model.CouponUserVo;
import com.qqtech.qquan.mall.enums.MallShopStyleDictEnum;
import com.qqtech.qquan.mall.model.MallProductSpecVo;
import com.qqtech.qquan.mall.model.MallProductVo;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallOrderService;
import com.qqtech.qquan.mall.service.MallProductService;
import com.qqtech.qquan.mall.service.MallProductSpecService;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.order.enums.OrderCouponDictTypeEnum;
import com.qqtech.qquan.order.model.OrderCouponVo;
import com.qqtech.qquan.order.model.OrderItemVo;
import com.qqtech.qquan.order.service.OrderCouponService;
import com.qqtech.qquan.user.enums.UserMoneyLogTypeDictEnum;
import com.qqtech.qquan.user.model.UserIdentityQuery;
import com.qqtech.qquan.user.model.UserIdentityVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserIdentityService;
import com.qqtech.qquan.user.service.UserMoneyLogService;

import jersey.repackaged.com.google.common.collect.Lists;

@Service
public class CouponUserServiceImpl extends BaseServiceImpl<CouponUser> implements CouponUserService {
	public static final Logger log = LoggerFactory.getLogger(CouponUserServiceImpl.class);

	@Resource
	private CouponUserDao couponUserDao;

	@Resource
	private CouponBaseService couponBaseService;

	@Resource
	private CouponSysParamCache couponSysParamCache;

	@Resource
	private CouponSysParamService couponSysParamService;

	@Resource
	private MallProductService mallProductService;
	
	@Resource
	private MallProductSpecService mallProductSpecService;

	@Resource
	private MallOrderService mallOrderService;

	@Resource
	private UserBaseService userBaseService;
	@Resource
	private UserIdentityService userIdentityService;

	@Resource
	private OrderCouponService orderCouponService;

	@Resource
	private UserMoneyLogService userMoneyLogService;
	
	@Resource
	private MallShopService mallShopService;
	
	
	@Override
	protected BaseDao<CouponUser> getBaseDao() {
		return couponUserDao;
	}

	@Override
	public CouponBaseVo getBaseByCouponNo(String couponNo) {
		CouponUserVo couponUserVo = couponUserDao.getBaseByCouponNo(couponNo);
		CouponBaseVo couponBaseVo = couponBaseService.getById(couponUserVo.getCouponId());
		return couponBaseVo;
	}

	@Override
	public int updateByCouponNo(String couponNo, CouponUserStatusDictEnum couponUserStatusDictEnum) {
		CouponUserVo couponUserVo = couponUserDao.getBaseByCouponNo(couponNo);
		if (couponUserVo.getStatus() != CouponUserStatusDictEnum.NOT_USED.getCode()) {
			return 0;
		}
		if (couponUserVo.getExpireAt() != null && couponUserVo.getExpireAt().before(TimeUtil.now())) {
			return 0;
		}
		CouponUserVo couponUser = new CouponUserVo();
		couponUser.setCouponNo(couponNo);
		couponUser.setStatus(couponUserStatusDictEnum.getCode());
		return couponUserDao.updateByCouponNo(couponUser);
	}
    @Override 
	public Integer getUnusedCount(Integer userId)
	{
		CouponUserVo couponUser = new CouponUserVo();
		couponUser.setUserId(userId);
		couponUser.setStatus(1);
		return couponUserDao.queryUnusedCount(couponUser);		
	}
	@Override
	public Integer addCouponUser(Integer userId, Integer couponId, CouponUserObtainWayDictEnum obtainWay,
			Integer obtainWayId) {
		int updateCount = couponBaseService.updateMinusQuantity(couponId, 1);
		if (updateCount > 0) {
			CouponBaseVo couponBaseVo = couponBaseService.getById(couponId);
			CouponUserVo couponUser = new CouponUserVo();
			couponUser.setUserId(userId);
			couponUser.setCouponNo(UUIDUtil.create());
			couponUser.setCouponId(couponId);
			couponUser.setObtainWay(obtainWay.getCode());
			couponUser.setObtainWayId(obtainWayId);
			couponUser.setExpireAt(couponBaseVo.getExpireAt());
			if (CouponBaseBizzTypeDictEnum.MONEY.getCode() == couponBaseVo.getBizzType()) {// 圈币
				couponUser.setStatus(CouponUserStatusDictEnum.BE_USED.getCode());
				if (null != couponBaseVo.getPrice() && couponBaseVo.getPrice().compareTo(BigDecimal.ZERO) > 0) {
					userMoneyLogService.addMoneyToUser(userId, couponBaseVo.getPrice().intValue(),
							"获得" + couponBaseVo.getPrice() + "圈币", UserMoneyLogTypeDictEnum.QUAN);
				}
			} else {
				couponUser.setStatus(CouponUserStatusDictEnum.NOT_USED.getCode());
			}
			return couponUserDao.insert(couponUser);
		}
		return null;
	}

	@Override
	public Page<CouponUserVo> queryPageListAdmin(CouponUser query, Pageable pageable) {
		return couponUserDao.queryPageListAdmin(query, pageable);
	}

	@Override
	public void addCouponUserBatch(List<Integer> userIds, Integer couponId, CouponUserObtainWayDictEnum obtainWay,
			Integer obtainWayId) {
		CouponBaseVo couponBaseVo = couponBaseService.getById(couponId);
		if (couponBaseVo != null && userIds != null && !userIds.isEmpty()) {
			int updateCount = couponBaseService.updateMinusQuantity(couponId, userIds.size());
			if (updateCount > 0) {
				List<CouponUser> couponUserList = new ArrayList<CouponUser>();
				for (int i = 0; i < userIds.size(); i++) {
					Integer userId = userIds.get(i);
					CouponUserVo couponUser = new CouponUserVo();
					couponUser.setUserId(userId);
					couponUser.setCouponNo(UUIDUtil.create());
					couponUser.setCouponId(couponId);
					couponUser.setStatus(CouponUserStatusDictEnum.NOT_USED.getCode());
					couponUser.setObtainWay(obtainWay.getCode());
					couponUser.setObtainWayId(obtainWayId);
					couponUser.setExpireAt(couponBaseVo.getExpireAt());
					couponUserList.add(couponUser);
				}
				if (!couponUserList.isEmpty()) {
					couponUserDao.insertInBatch(couponUserList);
				}
			}
		}
	}

	@Override
	public Result getMallOrderCouponList(Integer userId, List<OrderItemVo> orderItemVos, Integer couponUserId) {
		Result result = new Result();
		BigDecimal insideOrderTotal = new BigDecimal("0.00");
		BigDecimal outsideOrderTotal = new BigDecimal("0.00");
		BigDecimal total = new BigDecimal("0.00");// 订单总价
		BigDecimal totalMinu = new BigDecimal("0.00");
		/**
		 * 订单优惠信息
		 */
		List<OrderCouponVo> orderCouponList = new ArrayList<OrderCouponVo>();
		MallShopVo mallShopVo=null;
		List<CouponBaseVo> myCoupon = new ArrayList<CouponBaseVo>();
		CouponUserQuery couponUserQuery2 = new CouponUserQuery();
		couponUserQuery2.setBizzType(CouponBaseBizzTypeDictEnum.MALL_PRODUCT.getCode());
		couponUserQuery2.setUserId(userId);
		couponUserQuery2.setQueryExpireAt(TimeUtil.now());
		couponUserQuery2.setStatus(CouponUserStatusDictEnum.NOT_USED.getCode());
		couponUserQuery2.setId(couponUserId);
		couponUserQuery2.setGroup("userId,couponId");
		List<CouponUserVo> mallProductCouponList = couponUserDao.getLeftBase(couponUserQuery2);
		for (OrderItemVo orderItemVo : orderItemVos) {
			MallProductSpecVo mallProductSpecVo=mallProductSpecService.getById(orderItemVo.getProductId().intValue());
			MallProductVo mallProductVo=mallProductService.getById(mallProductSpecVo.getProductId());
			mallShopVo=mallShopService.getById(mallProductVo.getShopId());
			total = orderItemVo.getPrice().multiply(BigDecimal.valueOf(orderItemVo.getQuntity()));
			insideOrderTotal = insideOrderTotal.add(total);
			for (CouponUserVo couponUserVo : mallProductCouponList) {
				if (null != couponUserVo.getBizzId()
						&& couponUserVo.getBizzId() == orderItemVo.getProductId().intValue()) {// 0.01元购买商品
					CouponBaseVo couponBaseVo = new CouponBaseVo();
					couponBaseVo.setMinusPrice(orderItemVo.getPrice().subtract(couponUserVo.getPrice()));
					couponBaseVo.setName(couponUserVo.getName());
					couponBaseVo.setRemark(couponUserVo.getRemark());
					myCoupon.add(couponBaseVo);
					totalMinu = totalMinu.add(couponBaseVo.getMinusPrice());
					OrderCouponVo orderCouponVo = new OrderCouponVo();
					orderCouponVo.setMinusPrice(couponBaseVo.getMinusPrice());
					orderCouponVo.setRemark(couponUserVo.getRemark());
					orderCouponVo.setUserId(userId);
					orderCouponVo.setType(OrderCouponDictTypeEnum.MALL_PRODUCT.getCode());
					List<Integer> orderCouponUserIds = new ArrayList<Integer>();
					orderCouponUserIds.add(couponUserVo.getId());
					orderCouponVo.setCouponUserIds(orderCouponUserIds);
					orderCouponList.add(orderCouponVo);
					insideOrderTotal = insideOrderTotal.subtract(couponBaseVo.getMinusPrice());
				}
			}
		}
		// 商家优惠卷
		CouponUserVo couponUserVo = getMaxFullPriceMinusPrice(CouponBaseBizzTypeDictEnum.SHOP, userId,
				insideOrderTotal,mallShopVo.getId());
		if (null != couponUserVo) {
			totalMinu = putMaxMinusPrice(userId, totalMinu, orderCouponList, myCoupon, couponUserVo,OrderCouponDictTypeEnum.INSIDE);
		}
		total = insideOrderTotal.add(outsideOrderTotal);
		result.put("orderCouponList", orderCouponList);// 是否使用直供优惠卷
		result.put("totalPrice", total);// 订单原始总价
		result.put("totalMinu", totalMinu);// 优惠卷减少的总价
		result.put("payPrice", total.multiply(totalMinu));// 需要支付的金额
		result.put("myCoupon", myCoupon);// 我使用的优惠卷
		return result;
	}

	private BigDecimal putMaxMinusPrice(Integer userId, BigDecimal totalMinu, List<OrderCouponVo> orderCouponList,
			List<CouponBaseVo> myCoupon, CouponUserVo couponUserVo,OrderCouponDictTypeEnum orderCouponDictTypeEnum) {
		CouponBaseVo couponBaseVo = new CouponBaseVo();
		couponBaseVo.setRemark(couponUserVo.getRemark());
		couponBaseVo.setMinusPrice(couponUserVo.getMinusPrice());
		myCoupon.add(couponBaseVo);
		totalMinu = totalMinu.add(couponUserVo.getMinusPrice());
		OrderCouponVo orderCouponVo = new OrderCouponVo();
		orderCouponVo.setMinusPrice(couponBaseVo.getMinusPrice());
		orderCouponVo.setRemark(couponBaseVo.getRemark());
		orderCouponVo.setUserId(userId);
		orderCouponVo.setType(orderCouponDictTypeEnum.getCode());
		List<Integer> orderCouponUserIds = new ArrayList<Integer>();
		orderCouponUserIds.add(couponUserVo.getId());
		orderCouponVo.setCouponUserIds(orderCouponUserIds);
		orderCouponList.add(orderCouponVo);
		return totalMinu;
	}

	@Override
	public List<CouponUserVo> getHomemakingCoupon(Integer userId, Integer HomeMakingTypeId) {
		CouponUserQuery couponUserQuery = new CouponUserQuery();
		couponUserQuery.setUserId(userId);
		couponUserQuery.setStatus(CouponUserStatusDictEnum.NOT_USED.getCode());
		couponUserQuery.setGroup("userId,couponId");
		couponUserQuery.setQueryExpireAt(TimeUtil.now());
		couponUserQuery.setBizzType(CouponBaseBizzTypeDictEnum.HOME_MAKING_TYPE.getCode());
		couponUserQuery.setCategoryId(HomeMakingTypeId);
		List<CouponUserVo> couponUserList = couponUserDao.getLeftBase(couponUserQuery);
		return couponUserList;
	}

	/**
	 * 查询优惠券用户表的用户ids【去重了】
	 * 
	 * @param couponBaseBizzType
	 *            优惠券类型【1-普通优惠卷;2-商城某件产品(实物);3-服务技能(服务券);4-商城类别;5-商城】
	 * @param couponUserStatus
	 *            优惠券状态【1-未使用;2-已使用;3-已过期】
	 * @return 用户表的用户ids
	 */
	@Override
	public List<Integer> getUserIdsNoRepeatedByBizzType(CouponBaseBizzTypeDictEnum couponBaseBizzType,
			CouponUserStatusDictEnum couponUserStatus) {

		List<Integer> couponIds = couponBaseService.getCouponIdsByBizzType(couponBaseBizzType);// 优惠卷id列表
		if (couponIds == null || couponIds.isEmpty()) {
			return Lists.newArrayList();
		}
		CouponUserQuery couponUserQuery = new CouponUserQuery();
		couponUserQuery.setIncludeCouponIds(couponIds);// 包括的-优惠卷ids
		if (couponUserStatus != null) {// 如果为null，不设置状态
			couponUserQuery.setStatus(couponUserStatus.getCode());
		}
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "id"));
		List<CouponUserVo> couponUserList = couponUserDao.queryList(couponUserQuery, pageKit);
		List<Integer> userIds = Lists.newArrayList();
		if (couponUserList != null && couponUserList.size() > 0) {
			for (CouponUserVo couponUser : couponUserList) {
				if (!userIds.contains(couponUser.getUserId())) {
					userIds.add(couponUser.getUserId());
				}
			}
		}
		return userIds;
	}

	/**
	 * 查询--实物优惠券的用户--页数
	 * 
	 * @return
	 */
	@Override
	public int getUserTotalPage4MallProductCoupon(int pageSize) {
		int totalPage = 0;
		if (pageSize <= 0) {
			pageSize = PageKit.PAGE_SIZE;
		}
		List<Integer> userIds = getUserIdsNoRepeatedByBizzType(CouponBaseBizzTypeDictEnum.MALL_PRODUCT,
				CouponUserStatusDictEnum.NOT_USED);// 优惠券用户表中为实物的列表
		if (userIds.isEmpty()) {
			return 0;
		} else {
			int count = userIds.size();
			totalPage = count / pageSize;
			if (count % pageSize > 0) {
				totalPage += 1;
			}
		}
		return totalPage;
	}

	/**
	 * 查询--实物优惠券的用户--分页
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<UserIdentityVo> getUserList4MallProductCoupon(int pageNum, int pageSize) {
		if (pageNum <= 0) {
			pageNum = 1;
		}
		if (pageSize <= 0) {
			pageSize = PageKit.PAGE_SIZE;
		}
		List<Integer> userIds = getUserIdsNoRepeatedByBizzType(CouponBaseBizzTypeDictEnum.MALL_PRODUCT,
				CouponUserStatusDictEnum.NOT_USED);// 优惠券用户表中为实物的列表
		if (userIds.isEmpty()) {
			return Lists.newArrayList();
		}
		UserIdentityQuery userQ = new UserIdentityQuery();
		userQ.setIncludeUserIds(userIds);// 包括的用户ids
		Pageable pageKit = new PageKit(pageNum, pageSize, new PageOrder(Direction.ASC, "id"));
		return userIdentityService.queryList(userQ, pageKit);
	}

	/**
	 * 查询--家政服务优惠券的用户--总页数
	 * 
	 * @return
	 */
	@Override
	public int getUserTotalPage4HomeMakingCoupon(int pageSize) {
		int totalPage = 0;
		if (pageSize <= 0) {
			pageSize = PageKit.PAGE_SIZE;
		}
		List<Integer> userIds = getUserIdsNoRepeatedByBizzType(CouponBaseBizzTypeDictEnum.HOME_MAKING_TYPE, null);// 优惠券用户表中为家政服务的列表
		if (userIds.isEmpty()) {
			return 0;
		} else {
			int count = userIds.size();
			totalPage = count / pageSize;
			if (count % pageSize > 0) {
				totalPage += 1;
			}
		}
		return totalPage;
	}

	/**
	 * 查询--家政服务优惠券的用户--分页
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<UserIdentityVo> getUserList4HomeMakingCoupon(int pageNum, int pageSize) {
		if (pageNum <= 0) {
			pageNum = 1;
		}
		if (pageSize <= 0) {
			pageSize = PageKit.PAGE_SIZE;
		}
		List<Integer> userIds = getUserIdsNoRepeatedByBizzType(CouponBaseBizzTypeDictEnum.HOME_MAKING_TYPE, null);// 优惠券用户表中为家政服务的列表
		if (userIds.isEmpty()) {
			return Lists.newArrayList();
		}
		UserIdentityQuery userQ = new UserIdentityQuery();
		userQ.setIncludeUserIds(userIds);// 包括的用户ids
		Pageable pageKit = new PageKit(pageNum, pageSize, new PageOrder(Direction.ASC, "id"));
		return userIdentityService.queryList(userQ, pageKit);
	}

	@Override
	public List<CouponUserVo> getLeftBase(CouponUserQuery couponUserQuery) {
		return couponUserDao.getLeftBase(couponUserQuery);
	}

	@Override
	public void autoExpire() {
		couponUserDao.autoExpire();
	}

	@Override
	public int queryCountAdmin(CouponUser query) {
		return couponUserDao.queryCountAdmin(query);
	}

	@Override
	public List<CouponUserVo> queryListAdmin(CouponUser query, Pageable pageable) {
		return couponUserDao.queryListAdmin(query, pageable);
	}

	@Override
	public int queryCount4UserNoRepeat(CouponUser query) {
		return couponUserDao.queryCount4UserNoRepeat(query);
	}

	@Override
	public CouponUserVo getMaxFullPriceMinusPrice(CouponBaseBizzTypeDictEnum bizzType, Integer userId,
			BigDecimal fullPrice,Integer bizzId) {
		CouponUserQuery couponUserQuery = new CouponUserQuery();
		couponUserQuery.setBizzType(bizzType.getCode());
		couponUserQuery.setQueryExpireAt(TimeUtil.now());
		couponUserQuery.setQueryFullPrice(fullPrice);
		couponUserQuery.setStatus(CouponUserStatusDictEnum.NOT_USED.getCode());
		couponUserQuery.setUserId(userId);
		couponUserQuery.setBizzId(bizzId);
		return couponUserDao.getMaxFullPriceMinusPrice(couponUserQuery);
	}

	@Override
	public Result getQMallOrderCouponList(Integer userId, List<OrderItemVo> orderItemVos,Integer style) {
		Result result = new Result();
		BigDecimal insideOrderTotal = new BigDecimal("0.00");
		BigDecimal outsideOrderTotal = new BigDecimal("0.00");
		BigDecimal total = new BigDecimal("0.00");// 订单总价
		BigDecimal totalMinu = new BigDecimal("0.00");
		
		List<OrderCouponVo> orderCouponList = new ArrayList<OrderCouponVo>();
		/**
		 * 订单优惠信息
		 */
		List<CouponBaseVo> myCoupon = new ArrayList<CouponBaseVo>();
		
		MallShopVo mallShopVo=null;
		
		CouponUserQuery couponUserQuery2 = new CouponUserQuery();
		couponUserQuery2.setBizzType(CouponBaseBizzTypeDictEnum.MALL_PRODUCT.getCode());
		couponUserQuery2.setUserId(userId);
		couponUserQuery2.setQueryExpireAt(TimeUtil.now());
		couponUserQuery2.setStatus(CouponUserStatusDictEnum.NOT_USED.getCode());
		couponUserQuery2.setGroup("userId,couponId");
		List<CouponUserVo> mallProductCouponList = couponUserDao.getLeftBase(couponUserQuery2);
		for (OrderItemVo orderItemVo : orderItemVos) {
				total = orderItemVo.getPrice().multiply(BigDecimal.valueOf(orderItemVo.getQuntity()));
				insideOrderTotal = insideOrderTotal.add(total);
				MallProductSpecVo mallProductSpecVo=mallProductSpecService.getById(orderItemVo.getProductId().intValue());
				MallProductVo mallProductVo=mallProductService.getById(mallProductSpecVo.getProductId());
				mallShopVo=mallShopService.getById(mallProductVo.getShopId());
			for (CouponUserVo couponUserVo : mallProductCouponList) {
				if (null != couponUserVo.getBizzId()
						&& couponUserVo.getBizzId() == mallProductSpecVo.getProductId().intValue()) {// 0.01元购买商品
					CouponBaseVo couponBaseVo = new CouponBaseVo();
					couponBaseVo.setMinusPrice(orderItemVo.getPrice().subtract(couponUserVo.getPrice()));
					couponBaseVo.setName(couponUserVo.getName());
					couponBaseVo.setRemark(couponUserVo.getRemark());
					myCoupon.add(couponBaseVo);
					totalMinu = totalMinu.add(couponBaseVo.getMinusPrice());
					OrderCouponVo orderCouponVo = new OrderCouponVo();
					orderCouponVo.setMinusPrice(couponBaseVo.getMinusPrice());
					orderCouponVo.setRemark(couponUserVo.getRemark());
					orderCouponVo.setUserId(userId);
					orderCouponVo.setType(OrderCouponDictTypeEnum.MALL_PRODUCT.getCode());
					List<Integer> orderCouponUserIds = new ArrayList<Integer>();
					orderCouponUserIds.add(couponUserVo.getId());
					orderCouponVo.setCouponUserIds(orderCouponUserIds);
					orderCouponList.add(orderCouponVo);
					insideOrderTotal = insideOrderTotal.subtract(couponBaseVo.getMinusPrice());
				}
			}
		}
		String orderFirst = couponSysParamCache.get(CouponConst.PARAM_COUPON_ORDER_FIRST);
		// 周边商家优惠卷
		CouponUserVo couponUserVoTmp = getMaxFullPriceMinusPrice(CouponBaseBizzTypeDictEnum.SHOP, userId,
				insideOrderTotal,mallShopVo.getId());
		
		if(MallShopStyleDictEnum.LOCAL.getCode()==style){//周边商家
			boolean isToDayUseEd=false;
			if(YesNoEnum.YES.getCode()==Integer.valueOf(orderFirst)){
				isToDayUseEd = orderCouponService.isToday(userId, OrderCouponDictTypeEnum.NEARBY_MALL);// 周边商家
			}
			if(!isToDayUseEd){
				// 周边商家优惠卷
				CouponUserVo couponUserVo = getMaxFullPriceMinusPrice(CouponBaseBizzTypeDictEnum.NEARBY_MALL, userId,
						insideOrderTotal,null);
				if(null==couponUserVo){
					couponUserVo=couponUserVoTmp;
				}
				if(null!=couponUserVoTmp&&null!=couponUserVo&&couponUserVoTmp.getMinusPrice().compareTo(couponUserVo.getMinusPrice())>0){
					couponUserVo=couponUserVoTmp;
				}
				if (null != couponUserVo) {
					totalMinu = putMaxMinusPrice(userId, totalMinu, orderCouponList, myCoupon, couponUserVo,OrderCouponDictTypeEnum.NEARBY_MALL);
				}
			}
		}else if(MallShopStyleDictEnum.VIRTUAL.getCode()==style){//圈商商家
			boolean isToDayUseEd=false;
			if(YesNoEnum.YES.getCode()==Integer.valueOf(orderFirst)){
				isToDayUseEd = orderCouponService.isToday(userId, OrderCouponDictTypeEnum.Q_MALL);// 周边商家
			}
			if(!isToDayUseEd){
				// 圈商家优惠卷
				CouponUserVo couponUserVo = getMaxFullPriceMinusPrice(CouponBaseBizzTypeDictEnum.Q_MALL, userId,
						insideOrderTotal,mallShopVo.getId());
				if(null==couponUserVo){
					couponUserVo=couponUserVoTmp;
				}
				if(null!=couponUserVoTmp&&null!=couponUserVo&&couponUserVoTmp.getMinusPrice().compareTo(couponUserVo.getMinusPrice())>0){
					couponUserVo=couponUserVoTmp;
				}
				if (null != couponUserVo) {
					totalMinu = putMaxMinusPrice(userId, totalMinu, orderCouponList, myCoupon, couponUserVo,OrderCouponDictTypeEnum.Q_MALL);
				}
			}
		}else if(MallShopStyleDictEnum.QQSTORE.getCode()==style){//便利店
			boolean isToDayUseEd=false;
			if(YesNoEnum.YES.getCode()==Integer.valueOf(orderFirst)){
				isToDayUseEd = orderCouponService.isToday(userId, OrderCouponDictTypeEnum.CONVENIENT_MALL);// 周边商家
			}
			if(!isToDayUseEd){
				// 便利店优惠卷
				CouponUserVo couponUserVo = getMaxFullPriceMinusPrice(CouponBaseBizzTypeDictEnum.CONVENIENT_MALL, userId,
						insideOrderTotal,mallShopVo.getId());
				if(null==couponUserVo){
					couponUserVo=couponUserVoTmp;
				}
				if(null!=couponUserVoTmp&&null!=couponUserVo&&couponUserVoTmp.getMinusPrice().compareTo(couponUserVo.getMinusPrice())>0){
					couponUserVo=couponUserVoTmp;
				}
				if (null != couponUserVo) {
					totalMinu = putMaxMinusPrice(userId, totalMinu, orderCouponList, myCoupon, couponUserVo,OrderCouponDictTypeEnum.CONVENIENT_MALL);
				}
			}
		}
		
		total = insideOrderTotal.add(outsideOrderTotal);
		result.put("orderCouponList", orderCouponList);// 是否使用直供优惠卷
		result.put("totalPrice", total);// 订单原始总价
		result.put("totalMinu", totalMinu);// 优惠卷减少的总价
		result.put("payPrice", total.multiply(totalMinu));// 需要支付的金额
		result.put("myCoupon", myCoupon);// 我使用的优惠卷
		return result;
	}

	@Override
	public Integer queryCouponCount(Integer userId) {
		CouponUser query = new CouponUser();
		query.setUserId(userId);
		return couponUserDao.queryCountByUserId(query);
	}

	@Override
	public List<CouponUserVo> queryCouponCountByUserIds(List<Integer> userIds) {
		CouponUserQuery couponUserQuery = new CouponUserQuery();
		couponUserQuery.setUserIds(userIds);
		return couponUserDao.queryCountByUserIds(couponUserQuery);
	}
}